package cn.edu.neusoft.maxingxin231.yuejian06.Activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import cn.edu.neusoft.maxingxin231.yuejian06.Bean.RegisterBean;
import cn.edu.neusoft.maxingxin231.yuejian06.Listener.LoginListener;
import cn.edu.neusoft.maxingxin231.yuejian06.Model.RegisterModel;
import cn.edu.neusoft.maxingxin231.yuejian06.R;

public class RegisterActivity extends BaseActivity{
    EditText ed_num,ed_passwd,ed_passwdtwo,ed_email,ed_userphone;
    TextView ed_username;
    Button btn_register,btn_num;
    ImageButton imgbtn_return;
    LoginListener loginListener=new LoginListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
            if (registerBean.getSuccess().equals("1")){
                showToast("注册成功");
                returnMsg();
            }
            else {
                showToast("此人已注册");
            }
        }

        @Override
        public void onFail(String msg) {
            showToast("注册");
        }
    };

    @Override
    void initView() {//初始化
        setLayout_file(R.layout.activity_register);
        ed_username=(TextView) findViewById(R.id.ed_username);
        ed_num=(EditText) findViewById(R.id.ed_num);
        ed_passwd=(EditText) findViewById(R.id.ed_passwd);
        ed_passwdtwo=(EditText) findViewById(R.id.ed_passwdtwo);
        ed_email=(EditText) findViewById(R.id.ed_email);
        ed_userphone=(EditText) findViewById(R.id.ed_userphone);

        btn_num=(Button) findViewById(R.id.btn_num);
        btn_register=(Button) findViewById(R.id.btn_register);
        imgbtn_return=(ImageButton) findViewById(R.id.imgbtn_return);
        imgbtn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    void initEvent() {
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ed_username.getText().toString().trim().equals("") || ed_num.getText().toString().trim().equals("")
                        || ed_passwd.getText().toString().trim().equals("")||ed_passwdtwo.getText().toString().trim().equals("")){
                    showToast("不能为空");
                }else {
//                    ,String user_phone, String user_password, String user_name, String email
                    String user_name=ed_username.getText().toString();
                    String user_phone=ed_userphone.getText().toString();
                    String user_password=ed_num.getText().toString();
                    String user_passwordtwo=ed_passwdtwo.getText().toString();
                    String email=ed_email.getText().toString();
                    if (user_password.equals(user_passwordtwo)){
                        RegisterModel registerModel=new RegisterModel();
                        registerModel.getRegisterList(user_phone,user_password,user_name,email,loginListener);//拿着用户名和密码去服务器验证
                    }
                    else {
                        showToast("两次输入密码不一致");
                    }
                }
            }
        });
        //点击事件
    }

    @Override
    void initData() {

    }
    void returnMsg(){
        Intent intent=new Intent();
        String username=ed_username.getText().toString();
        intent.putExtra("user",username);
        setResult(RESULT_OK,intent);
        finish();
    }
}
